package com.jims.master.common.utils;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;

/**
 * 邮件发送类
 * @author zy
 * @version 2015-06-23
 */
public class SendMail
{
    /**
     *测试邮件
     */
    public static void main(String[] args){
        try {
            SendMail.sendMail("812173473@qq.com", "测试", "测试");
          // SendMail.sendFileMail("812173473@qq.com", "测试", "<html><head></head><body><h1>你好：附件！！</h1></body></html>","D:/说明.txt","测试.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *发送邮件
     * toPath 收件人地址
     * title  邮件标题
     * con   邮件内容
     * filePath 附件路径
     * fileName 附件名
     */
    public static void sendFileMail(String toPath,String title,String con,String filePath,String  fileName)throws Exception {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost(CommonUtil.mailHost());
        senderImpl.setUsername(CommonUtil.mailUserName());
        senderImpl.setPassword(CommonUtil.mailPassword());
        // 建立HTML邮件消息
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        // true表示开始附件模式
        MimeMessageHelper messageHelper = null;
        messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
        // 设置收件人，寄件人
        messageHelper.setTo(toPath);
        messageHelper.setFrom(CommonUtil.mailFrom());
        messageHelper.setSubject(title);
        // true 表示启动HTML格式的邮件
        messageHelper.setText(con, true);
        FileSystemResource file1 = new FileSystemResource(new File(filePath));
       //附件名有中文可能出现乱码
        messageHelper.addAttachment(MimeUtility.encodeWord(fileName), file1);
        // 发送邮件
        senderImpl.send(mailMessage);
        System.out.println("邮件发送成功.....");
    }

    /**
     *发送邮件
     * toPath 收件人地址
     * title  邮件标题
     * con   邮件内容
     */
    public static void sendMail(String toPath,String title,String con)throws MessagingException {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost(CommonUtil.mailHost());
        senderImpl.setUsername(CommonUtil.mailUserName());
        senderImpl.setPassword(CommonUtil.mailPassword());
        // 建立HTML邮件消息
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        // true表示开始附件模式
        MimeMessageHelper messageHelper = null;
        messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
        // 设置收件人，寄件人
        messageHelper.setTo(toPath);
        messageHelper.setFrom(CommonUtil.mailFrom());
        messageHelper.setSubject(title);
        // true 表示启动HTML格式的邮件
        messageHelper.setText(con, true);
        // 发送邮件
        senderImpl.send(mailMessage);
        System.out.println("邮件发送成功.....");
    }
}